var updateDeptDialog = new Dialog( {
	showdiv :"updateDeptDialog",
	title :"修改部门信息",
	x :560,
	y :360
});
rootDialog.childDialog = updateDeptDialog;
var createDeptDialog = new Dialog( {
	showdiv :"createDeptDialog",
	title :"创建部门信息",
	x :560,
	y :360
});
rootDialog.childDialog = createDeptDialog;
function initDeptSite(deptid) {
	Dept.findDeptByMdeptidDownUser(deptroot, function(data) {
		DWRUtil.addOptions("mdeptid", data, "deptid", "dept");
		if (deptid != null && deptid != "") {
			Dept.chakan(deptid,
					function(data) {
						for ( var i = 0; i < data.length; i++) {
							if (data[i].tname == "sign") {
								DWRUtil.setValue(data[i].tname, data[i].tval);
							} else {
								if (data[i].tname == "mdeptid") {
									DWRUtil.setValue(data[i].tname,
											data[i].tval);
								} else {
									if (data[i].tname == "siteid") {
									} else {
										DWRUtil.setValue(data[i].tname,
												data[i].tvalue);
									}
								}
							}
						}
					});
		}
	});
}
function newDept() {
	var dept1 = trim_value(DWRUtil.getValue("dept"));
	var sign = DWRUtil.getValue("sign");
	var depttext = trim_value(DWRUtil.getValue("depttext"));
	var deptid = DWRUtil.getValue("deptid");
	var mdeptid = DWRUtil.getValue("mdeptid");
	var siteid = URLParams["siteid"];
	var checkpname = true;
	Dept.checkPName(dept1, deptid, mdeptid, function(data) {
		if (data == 1) {
			checkpname = true;
			$Q("depterror").innerHTML = "正确！";
		} else {
			checkpname = false;
			$Q("depterror").innerHTML = "部门名称已存在！";
		}
		if (depttext.length > 120) {
			alert("部门描述最多填写120个英文字符或60个中文字符！");
			checkpname = false;
		}
		if (checkpname) {

			if (deptid != null && deptid != "") {
				Dept.updateDept(deptid, dept1, depttext, sign, mdeptid,
						confirm("是否继承上级部门角色?"), function(data) {
							if (data == 1) {
								send(dept);
								alert("修改部门信息成功！");
							} else {
								alert("修改部门信息失败！");
							}
							closeDialog(updateDeptDialog);
						});
			} else {
				Dept.newDept(dept1, depttext, sign, mdeptid,
						confirm("是否继承上级部门角色?"), function(data) {
							if (data == 1) {
								send(dept);
								alert("新建部门信息成功！");
							} else {
								alert("新建部门信息成功！");
							}
							closeDialog(createDeptDialog);
						});
			}

		}
	});
}
var chakancellfuncs = [ function(data) {
	return data.title;
}, function(data) {
	return data.tvalue;
} ];
var xiugaicellfuncs = [ function(data) {
	return data.title;
}, function(data) {
	var inputtext;
	if (data.tname == "sign") {
		inputtext = document.createElement("span");
		inputtext.setAttribute("id", "signdiv");
		var sign0 = document.createElement("input");
		sign0.setAttribute("type", "radio");
		sign0.setAttribute("name", data.tname);
		sign0.setAttribute("id", data.tname);
		sign0.setAttribute("value", "1");
		var sign1 = document.createElement("input");
		sign1.setAttribute("type", "radio");
		sign1.setAttribute("name", data.tname);
		sign0.setAttribute("id", data.tname);
		sign1.setAttribute("value", "0");
		inputtext.appendChild(document.createTextNode("\u6709\u6548"));
		inputtext.appendChild(sign0);
		inputtext.appendChild(document.createTextNode("\u65e0\u6548"));
		inputtext.appendChild(sign1);
	} else {
		inputtext = document.createElement("input");
		inputtext.setAttribute("type", "text");
		inputtext.setAttribute("name", data.tname);
		inputtext.setAttribute("value", data.tvalue);
	}
	return inputtext;
} ];
var cellfuncs = [
		function(data) {
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				rootDialog.showdivWidth = 450;
				rootDialog.showdivHeight = 250;
				rootDialog.title = "\u67e5\u770b\u7ec4\u7ec7\u673a\u6784\u4fe1\u606f";
				openDialog(rootDialog, "", function() {
					var chakantable = document.createElement("table");
					chakantable.style.align = "center";
					chakantable.className = "table";
					rootDialog.contentdiv.appendChild(chakantable);
					var chakantbody = document.createElement("tbody");
					chakantbody.setAttribute("id", "chakantbody");
					chakantable.appendChild(chakantbody);
					Dept.chakan(data.deptid, function(data) {
						DWRUtil.removeAllRows("chakantbody");
						DWRUtil.addRows("chakantbody", data, chakancellfuncs,
								tableclass);
					});
				});
			};
			href.innerHTML = data.dept;
			return href;
		},
		function(data) {
			return data.depttext;
		},
		function(data) {
			return data.deptlevel;
		},
		function(data) {
			return data.sign == "1" ? "有效" : "无效";
		},
		function(data) {
			// return data.mdept;
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				deptroot = data.deptid;
				dept = {
					navigationsize :10,
					paramname :"dept",
					page :1,
					funcname :"Dept.findByDeptPage('" + data.deptid + "',)",
					cellfuncs :cellfuncs,
					pagesize :10,
					count :"Dept.getCountByMDept('" + data.deptid + "',)",
					idname :"deptlistZone"
				};
				send(dept);
			};
			href.innerHTML = "下级部门";
			return href;
		},
		function(data) {
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				initSelectUsers(data.deptid, "dept");
			};
			href.innerHTML = "\u7528\u6237\u5217\u8868";
			return href;
		},
		function(data) {
			if (!deptroot && data.useruuid) {
				return "";
			}
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				initSelectRoles(data.deptid + ":" + data.mdeptid, "dept");
			};
			href.innerHTML = "\u89d2\u8272\u5217\u8868";
			return href;
		},
		function(data) {
			if (!deptroot && data.useruuid) {
				return "";
			}
			var href = document.createElement("a");
			href.setAttribute("href", "#");
			href.onclick = function() {
				openDialog(updateDeptDialog, "add.htm", function() {
					initDeptSite(data.deptid);
				});
			};
			href.innerHTML = "\u4fee\u6539";
			return href;
		},
		function(data) {
			if ((deptroot && data.deptid != "1") || !data.useruuid) {
				var href = document.createElement("a");
				href.setAttribute("href", "#");
				href.onclick = function() {
					if (confirm("\u786e\u8ba4\u5220\u9664\u7ec4\u7ec7\u673a\u6784("
							+ data.dept + ")\u5417?")) {
						Dept.removeDeptById(data.deptid, function(data) {
							send(dept);
							alert(data);
						});
					}
				};
				href.innerHTML = "\u5220\u9664";
				return href;
			} else {
				return "";
			}
		} ];
var deptroot = "";
var dept = {
	navigationsize :10,
	paramname :"dept",
	page :1,
	funcname :"Dept.findByDeptPage('',)",
	cellfuncs :cellfuncs,
	pagesize :10,
	count :"Dept.getCountByMDept('',)",
	idname :"deptlistZone"
};
var pageuuid = dept.paramname;
window.onload = function() {
	initDWR();
	useLoading("../../expimages/blue-loading.gif", "img");
	if (deptroot) {
		$('backahref').style.display = "none";
	} else {
		$('backahref').style.display = "";
	}
	Dept.checkPopAnd(8, function(data) {
		if (data) {
			send(dept);
		} else {
			alert("请勿非法访问，仅拥有（权限管理）权限的用户可以访问！")
			top.window.location = URLParams['basePath'];
		}
	});
};
function goDeptUp() {
	if (deptroot) {
		Dept.getdeptup(deptroot, function(data) {
			deptroot = data;
			dept = {
				navigationsize :10,
				paramname :"dept",
				page :1,
				funcname :"Dept.findByDeptPage('" + deptroot + "',)",
				cellfuncs :cellfuncs,
				pagesize :10,
				count :"Dept.getCountByMDept('" + deptroot + "',)",
				idname :"deptlistZone"
			};
			send(dept);

		});
	} else {
		alert("已返回到根组织机构!");
	}
}